(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('waf.workflow.commandErrorController', commandErrorController);
	commandErrorController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ajaxServiceFactory' ];
	function commandErrorController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, taskInfo = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("wfSetupVerifyError");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			var params = {
					taskId : taskInfo.taskId
			};
			var ajaxService = ajaxServiceFactory.buildAjaxService({
				commandError : {
					url : 'do/' + taskInfo.serviceCode + '/workflow/commandError/' + taskInfo.dataType,
					method : 'post'
				}
			})
			ajaxService.commandError(params).then(function(response) {
				ctrl.task = response.data.task || {};
			});
		};
		
		
		ctrl.process = function() {
			var ajaxService = ajaxServiceFactory.buildAjaxService({
				errorProcess : {
					url : 'do/' + taskInfo.serviceCode + '/workflow/commandError/process/' + taskInfo.dataType,
					method : 'post'
				}
			})
			ajaxService.errorProcess(ctrl.task).then(processSuccessful, processFailed);
		};
		
		function processSuccessful(response) {
//			$scope.go('waf.home');
			var state;
			if (taskInfo.serviceCode == 'CDA') {
				state = "cda.workflow.wfSetup.list";
			} else if (taskInfo.serviceCode == 'CLS'){
				state = "cls.workflow.wfTxn.list";
			} else if (taskInfo.serviceCode == 'APF'){
				state = "apf.workflow.wfTxn.list";
			} else if (taskInfo.serviceCode == 'LFE'){
				state = "lfe.workflow.wfTxn.list";
			} else {
				$scope.go('waf.home');
			}
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.process' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go(state);
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : state,
					'params' : {}
				}]
			});
		}

		function processFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);